<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Seller_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    public function get_category_name($category_id)
    {
        $result = $this->db->where('category_id',$category_id)->get('categories');
        return $result;  
    }
    public function is_that_category_has_product($category_id)
    {
        /*if($this->db->where('category_id',$category_id)->get('products')->num_rows()>0)
        {
            $result = $this->db->where('category_id',$category_id)->get('products')->result();
            foreach($result as $row)
            {
                // product id yi al sellerinkiyle check ettir
                $product_id = $row->product_id;
                if($this->db->where('product_id',$product_id)->get('seller')->num_rows()>0)
                {
                    return true;
                }
            }
            return false;
        }*/
        
        
        $this->db->select('*');
                $this->db->from('products');
                $this->db->join('seller', 'seller.product_id = products.product_id');
                $this->db->where(array('category_id' => $category_id));
                $this->db->where('seller.mod <>',2);
                $result = $this->db->get();
                
                return $result;
        
    }

    public function get_product_numbers_by_category_id($category_id)
    {
      /*  if($this->db->where('category_id',$category_id)->get('products')->num_rows()>0)
        {
            $result = $this->db->where('category_id',$category_id)->get('products')->result();
            foreach($result as $row)
            {
                // product id yi al sellerinkiyle check ettir
                $product_id = $row->product_id;
                if($this->db->where('product_id',$product_id)->get('seller')->num_rows()>0)
                {
                    return $this->db->where('product_id',$product_id)->get('seller')->num_rows();
                }
            }
           
        }*/
        
                //$result = $this->db->where(array('category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->result();
        
        $sql = "select  seller_id from products, seller where seller.mod != 2 and products.category_id = ? and  products.product_id = seller.product_id ";
        $result = $this->db->query($sql,$category_id);
        return $result;
    }
    public function is_that_subcategory_has_product($subcategory_id)
    {
       /* if($this->db->where('subcategory_id',$subcategory_id)->get('products')->num_rows()>0)
        {
            $result = $this->db->where('subcategory_id',$subcategory_id)->get('products')->result();
            foreach($result as $row)
            {
                // product id yi al sellerinkiyle check ettir
                $product_id = $row->product_id;
                if($this->db->where('product_id',$product_id)->get('seller')->num_rows()>0)
                {
                    return true;
                }
            }
            return false;
        }*/
        
                
        
        $this->db->select('*');
                $this->db->from('products');
                $this->db->join('seller', 'seller.product_id = products.product_id');
                $this->db->where(array('subcategory_id' => $subcategory_id));
                $this->db->where('seller.mod <>',2);
                $result = $this->db->get();
                
                return $result;
        
    }
    public function is_that_brand_has_product($category_id,$subcategory_id,$brand_id)
    {
        if($this->db->where(array('subcategory_id' => $subcategory_id, 'category_id' => $category_id, 'brand_id' => $brand_id))->get('products')->num_rows()>0)
        {
            $result = $this->db->where(array('subcategory_id' => $subcategory_id, 'category_id' => $category_id, 'brand_id' => $brand_id))->get('products')->result();
            foreach($result as $row)
            {
                // product id yi al sellerinkiyle check ettir
                $product_id = $row->product_id;
                if($this->db->where(array('product_id'=>$product_id,'mod <>' => 2))->get('seller')->num_rows()>0)
                {
                    return true;
                }
            }
            return false;
           
        }
        
    }         
    public function get_product_numbers_by_subcategory_id($subcategory_id)
    {
        $this->db->select('*');
                $this->db->from('products');
                $this->db->join('seller', 'seller.product_id = products.product_id');
                $this->db->where(array('subcategory_id' => $subcategory_id));
                $this->db->where('seller.mod <>',2);
                $result = $this->db->get();
        
  /* $sql = "select  seller_id from products, seller where  products.subcategory_id = ? and products.product_id = seller.product_id ";
        $result = $this->db->query($sql,$subcategory_id);*/
        return $result;
    }
    public function get_product_numbers_by_brand_id($category_id,$subcategory_id,$brand_id)
    {
  /* $sql = "select  seller_id from products, seller where products.category_id = ? and products.brand_id = ? and products.subcategory_id = ? and products.product_id = seller.product_id ";
        $result = $this->db->query($sql,array($category_id,$brand_id,$subcategory_id));
        return $result;*/
        
        
            $this->db->select('*');
                $this->db->from('products');
                $this->db->join('seller', 'seller.product_id = products.product_id');
                $this->db->where(array('subcategory_id' => $subcategory_id, 'category_id' => $category_id , 'brand_id' => $brand_id));
                $this->db->where('seller.mod <>',2);
                $result = $this->db->get();
    }
    
    public function get_brands_by_category_and_subcategory($category_id,$subcategory_id)
    {
      /*  $this->db->select('brand_id');
        $this->db->distinct('brand_id');
                $this->db->from('products');
                $this->db->join('seller', 'seller.product_id = products.product_id');
                $this->db->where(array('subcategory_id' => $subcategory_id, 'category_id' => $category_id ));
                $result = $this->db->get();*/
        //$result = $this->db->where(array('category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->result();
        
        $sql = "select DISTINCT brand_id from products, seller where seller.mod != 2 and products.category_id = ? and products.subcategory_id = ? and products.product_id = seller.product_id ";
        $result = $this->db->query($sql,array($category_id,$subcategory_id));
        return $result;       
    }  
    public function update_stock($cart_id)
    {
        $products = $this->db->where('cart_id',$cart_id)->get('cart_products');
        if($products->num_rows() > 0 )
        {
            foreach($products->result() as $product )
            {
                $seller_id = $product->seller_id;
                $quantitiy = $product->quantitiy;
                $seller_info = $this->db->where(array('seller_id' => $seller_id, 'status' => 1 ))->get('seller');
                if($seller_info->num_rows()> 0)
                {
                    $stock = $seller_info->row(0)->stock;
                    if($stock == $quantitiy)
                    {
                       // ürün bitti
                       if($this->db->where('seller_id',$seller_id)->update('seller',array('stock' => 0,'status' => 0)))
                       {
                            return true;
                       }
                    }
                    else if($stock >= $quantitiy)
                    {
                        // stock - quantity
                        
                        $new_quant = ($stock - $quantitiy);
                       if($this->db->where('seller_id',$seller_id)->update('seller',array('stock' => $new_quant)))
                       {
                            return $new_quant;
                       }                        
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }    
        }
        else
        {
            return false;
        }
        return true;
    }    
    public function get_unit_price($adet,$seller_id)
    {
       $seller_info = $this->db->where('seller_id',$seller_id)->get('seller');
       if($seller_info->num_rows() > 0)
       {
         $seller = $seller_info->row(0);
         if($seller->fiyat_araligi_id == "0")
         {
            return $seller->user_price;
         }
         else
         {
            $fiyat_araligi_id = $seller->fiyat_araligi_id;
            $araliklar = $this->db->where('fiyat_araligi_id',$fiyat_araligi_id)->get('fiyat_araligi')->row(0);
            if($adet >= $araliklar->aralik21)
            {
                return $araliklar->fiyat2;
            }
            else
            {
                return $seller->user_price;
            }
         }
         
       }
       else
       {
            return false;
       } 
    }  
        


}
?>